package com.zgjkhis.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.logging.aop.log.Log;
import com.zgjkhis.modules.security.security.LoginUserUtil;
import com.zgjkhis.modules.system.domain.BaseUse;
import com.zgjkhis.modules.system.service.BaseUseService;
import com.zgjkhis.modules.system.service.dto.BaseUseQueryCriteria;
import com.zgjkhis.modules.util.CharUtil;
import com.zgjkhis.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @date 2018-11-23
 */
@Api(tags = "基础管理：用法管理")
@RestController
@RequestMapping("/use")
public class BaseUseController {

    private static final String ENTITY_NAME = "baseUse";
    private final BaseUseService baseUseService;

    public BaseUseController(BaseUseService baseUseService) {
        this.baseUseService = baseUseService;
    }

    @Log("新增用法")
    @ApiOperation("新增用法")
    @PostMapping(value = "/add")
//    @PreAuthorize("@el.check('supplier:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody BaseUse resources) {
        if (resources.getId() != null) {
            throw new ServiceException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        if (resources.getType() != 1) {
            resources.setCreateUser(SecurityUtils.getUserId().toString());
            resources.setPharmacyId(LoginUserUtil.getPharmId());
        }
        if (SecurityUtils.getUserId() != 1 && resources.getType() == 1) {
            throw new ServiceException("权限不够,创建失败");
        }

        return new ResponseEntity<>(baseUseService.save(resources), HttpStatus.OK);
    }

    @Log("修改用法")
    @ApiOperation("修改用法")
    @PostMapping(value = "/update")
//    @PreAuthorize("@el.check('supplier:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody BaseUse resources) {
        if (SecurityUtils.getUserId() != 1 && resources.getType() == 1) {
            throw new ServiceException("权限不够,修改失败");
        }
        BaseUse use = baseUseService.getById(resources.getId());
        if (!use.getCreateUser().equals(SecurityUtils.getUserId().toString())) {
            throw new ServiceException("权限不够,修改失败");
        }
        baseUseService.updateById(resources);
        return ResponseEntity.ok(true);
    }

    @Log("查询用法")
    @ApiOperation("查询用法")
    @PostMapping(value = "/getList")
//    @PreAuthorize("@el.check('supplier:list')")
    public ResponseEntity<Object> getSuppliers(BaseUseQueryCriteria baseUseDto) {
        LambdaQueryWrapper<BaseUse> queryWrapper = new LambdaQueryWrapper<>();
        if (!CharUtil.isEmpty(baseUseDto.getStatus())) {
            queryWrapper.eq(BaseUse::getStatus, baseUseDto.getStatus());
        }
        if (!CharUtil.isEmpty(baseUseDto.getUseAllClinic())) {
            queryWrapper.like(BaseUse::getUseAllClinic, baseUseDto.getUseAllClinic());
        }
        queryWrapper.and(wrapper -> wrapper.eq(BaseUse::getType, 1).or().eq(BaseUse::getPharmacyId, LoginUserUtil.getPharmId()));
        List<BaseUse> list = baseUseService.list(queryWrapper);
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

}
